package net.apps.std;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.apps.core.ApplFactory;
import net.apps.core.ApplManagerRaw;
import net.apps.core.Channel;
import net.apps.core.Connection;
import net.apps.core.Message;
import net.apps.core.RawAppl;
import net.apps.core.meta.Meta;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * Provides actions from keyboard or similar hardware
 * 
 * @author Maxim Kizub
 * 
 */

public class Keyboard implements RawAppl, KeyListener, WindowListener {

	private final ApplManagerRaw amgr;
	private final TextFrame frame;
	private final TextMessageProtocol protocol;
	
	private boolean exiting;

	public Keyboard(ApplManagerRaw amgr) {
		this.amgr = amgr;
		this.frame = new TextFrame();
		this.frame.addWindowListener(this);
		this.protocol = (TextMessageProtocol)ApplFactory.makeProtocol("net.apps.std.Text");
	}
	
	

	@Override
	public void main(ApplManagerRaw amgr) throws InterruptedException {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frame.pack();
				frame.setLocationByPlatform(true);
				frame.setVisible(true);
			}
		});

		Connection conection = amgr.getConnection();
		Channel channel = conection.getChannel(0);
		
		for (;;) {
			Message msg = channel.take(0);
			if (msg != null) {
				Meta mmeta = msg.getMessageMeta();
				Meta pmeta = mmeta.getParent();
				if (pmeta.getName().equals("net.apps.std.ApplCtrl")) {
					if (mmeta.getId() == ApplCtrlProtocol.ID_MSG_HELO) {
						; //
					}
					else if (mmeta.getId() == ApplCtrlProtocol.ID_MSG_EXIT) {
						return;
					}
				}
			}
			if (exiting)
				return;
			amgr.reportIdle(this);
		}
	}
	
	
	@Override
	public void keyTyped(KeyEvent e) {
		frame.label.setText("key typed: '"+e.getKeyChar()+"'");
		String text = Character.toString(e.getKeyChar());
//		EditTextProtos.Generic proto_msg = EditTextProtos.Generic.newBuilder()
//				.setText(text)
//				.build();
//		Message msg = new Message(proto_msg);
		Message msg = protocol.new TextMessage(text);
		Channel pchannel = amgr.getConnection().getChannel(0);
		if (pchannel != null)
			pchannel.push(msg, 0);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		frame.label.setText("key pressed: "+KeyEvent.getKeyText(e.getKeyCode()));
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void windowOpened(WindowEvent e) {
	}

	@Override
	public void windowClosing(WindowEvent e) {
		exiting = true;
		amgr.requestWakeup(this);
	}

	@Override
	public void windowClosed(WindowEvent e) {
		exiting = true;
		amgr.requestWakeup(this);
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}


	@SuppressWarnings("serial")
	private class TextFrame extends JFrame {
		private JLabel label;
		public TextFrame() {
			super("Keyboard");
			JPanel panel = new JPanel();
			panel.setLayout(new BorderLayout());
			panel.setFocusable(true);

			label = new JLabel();
			label.setHorizontalAlignment(JLabel.CENTER);

			panel.addKeyListener(Keyboard.this);
			panel.add(label, BorderLayout.CENTER);

			setPreferredSize(new Dimension(300, 100));
			getContentPane().add(panel);
		}

	}

}
